{template 'common/header'}
{template '_header'}
<title>商品详情</title>
<style type="text/css">
    body {margin:0px; background:#efefef; font-family:'微软雅黑'; -moz-appearance:none;}
    a {text-decoration:none;}

    .address_addnav {height:44px; width:94%; padding:0 3%; border-bottom:1px solid #f0f0f0; border-top:1px solid #f0f0f0; margin-top:14px; line-height:42px; color:#666; background:#fff;}
    .address_list {height:50px; padding:0 10px;  border-bottom:1px solid #f0f0f0; border-top:1px solid #f0f0f0; margin-top:14px; background:#fff;}
    .address_list .ico {height:50px; width:30px;   float:left; color:#999;margin-right:-30px; z-index:2}
    .address_list .ico i { font-size:24px;margin-top:15px;margin-left:10px;z-index:2;position: relative;}
    .address_list .info {height:50px; width:100%; float:left;position: relative;}
    .address_list .info .inner { margin-left:40px;margin-right:50px;}
    .address_list .info .inner .addr {height:20px; width:100%; color:#999; line-height:26px; font-size:14px; overflow:hidden;}
    .address_list .info .inner .user {height:30px; width:100%; color:#666; line-height:30px; font-size:16px; overflow:hidden;}
    .address_list .info .inner .user span {color:#444; font-size:16px;}
    .address_list .btn { width:45px; float:right;margin-left:-45px;z-index:2;position: relative;}
    .address_list .btn .edit,.address_list .btn .remove {height:50px; float:right; color:#999; font-size:18px;margin-top:5px;}
    .address_list .btn .edit { margin-right:10px;}
    
.address_addnav {height:40px;  border-bottom:1px solid #f0f0f0; border-top:1px solid #f0f0f0; margin-top:14px; line-height:40px; color:#666; }
.address_main {height:auto;width:94%; padding:0px 3%; border-bottom:1px solid #f0f0f0; border-top:1px solid #f0f0f0; margin-top:14px; background:#fff;}
.address_main .line {height:44px; width:100%; border-bottom:1px solid #f0f0f0; line-height:44px;}

.address_main .line input {float:left; height:44px; width:100%; padding:0px; margin:0px; border:0px; outline:none; font-size:16px; color:#666;padding-left:5px;}
.address_main .line select  { border:none;height:25px;width:100%;color:#666;font-size:16px;}
.address_sub1 {height:44px; margin:14px 10px; background:#ff4f4f; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}
.address_sub2 {height:44px; margin:14px 10px; background:#ddd; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#666; border:1px solid #d4d4d4;}
</style>
<script type="text/javascript" src="../addons/ewei_shop/static/js/dist/area/cascade.js"></script>
<div id='container'></div>
<script type='text/html' id='tpl_main'>
    <div id="showform">
    <div class="page_topbar">
    <a href="javascript:;" class="back" onclick="history.back()"><i class="fa fa-angle-left"></i></a>
    <a href="{php echo $this->createPluginMobileUrl('goldshop/lists')}" class="btn" ><i class="fa fa-home"></i></a>
    <div class="title">商品详情</div>
    </div>
      <div class="info_top">
    	<img src="<%goods.thumb%>"/>
        <div class="info">
        	<div class="classs"><%goods.subtitle%></div>
            <div class="name"><%goods.title%></div>
        </div>
      </div>
        <div class="info_price">
            <div class="num">
            <%if goods.acttype==0%>
                 <%goods.credit%><i class="fa fa-database"></i> <i class="fa fa-plus" style='color:#999;'></i> <%goods.money%><i class="fa fa-rmb"></i>
            <%/if%>
            <%if goods.acttype==1%>
               <%goods.credit%><i class="fa fa-database"></i>
            <%/if%>
    
            <%if goods.acttype==2%>
               <%goods.money%><i class="fa fa-rmb"></i>
            <%/if%>
            </div>
            <div class="<%if goods.canbuy%>sub<%else%>sub2<%/if%>" id="btnsub"  onclick="addaddress()">
                     <%if goods.canbuy%>
                       <%if goods.type=='1'%>立即抽奖<%else%>立即兑换<%/if%>
                       <%else%>
                       <%goods.buymsg%>
                       <%/if%>
            </div>
         
        </div>
        <div class="info_content">
               <div class="info_timestate" <%if goods.timestate==''%>style="display:none"<%/if%>></div>
         
            <%if goods.goodsdetail!=''%>
            <div class='ctitle'>商品简介:</div>
            <%=goods.goodsdetail%>
            <%/if%>
            
            
            <div class='ctitle'><%if goods.type=='1'%>活动<%else%>使用<%/if%>范围:</div>
            <div class='ccontent'><%if goods.area==''%>全国<%else%><%goods.area%><%/if%> <%if goods.dispatch>0%><span style='color:#f90'>(需额外支付运费<%goods.dispatch%>元)</span><%/if%></div>
            
            <%if goods.isendtime=='1'%>
            <div class='ctitle'><%if goods.type=='1'%>活动<%else%>使用<%/if%>有效期:</div>
            <div class='ccontent'>兑换之日起至<%goods.endtime_str%></div>
            <%/if%>
            
            <div class='ctitle'>兑换流程:</div>
            <%=goods.detail%>
            
            <%if goods.noticedetail!=''%>
              <div class='ctitle'>注意事项:</div>
                <%=goods.noticedetail%>
            <%/if%>
            
            <%if goods.subdetail!=''%>
              <div class='ctitle'>商家介绍:</div>
            <%=goods.subdetail%>
            <%/if%>
      <fieldset class="info_bottom">
                        <legend>重要说明</legend>
           
                        <div class=content>
                        商品兑换流程请仔细参照商品详情页的"兑换流程"、"注意事项"与"使用时间"，除商品本身不能正常兑换外，商品一经兑换，一律不退还积分。（如商品过期、兑换流程操作失误、仅限新用户兑换)
                        </div>
                <div class=footer>
                 活动由{$shop['name']}提供，与商品生产公司无关。
                </div>
       
     </fieldset>  
        </div>
</div>
        <div  id="addform" style="display:none;">
             <div class="page_topbar">
               <a href="javascript:;" class="back" id="editback" onclick="window.location.reload()"><i class="fa fa-angle-left"></i></a>
               <div class="title">编辑收货地址</div>
           </div>

           <div class="address_main" >
               <input type='hidden' id='addressid' value="<%goods.id%>"/>
               <div class="line"><input type="text" placeholder="收件人" id="realname" value="" /></div>
               <div class="line"><input type="text" placeholder="联系电话"  id="mobile" value=""/></div>

               <div class="line"><select id="sel-provance" onchange="selectCity();" class="select"><option value="" selected="true">所在省份</option></select></div>
                <div class="line"><select id="sel-city" onchange="selectcounty()" class="select"><option value="" selected="true">所在城市</option></select></div>
                <div class="line"><select id="sel-area" class="select"><option value="" selected="true">所在地区</option></select></div>

               <div class="line"><input type="text" placeholder="详细地址"  id="address" value=""/></div>
           </div>
           <div class="address_sub1" id="btnsubaddress">确认</div>
           <div class="address_sub2" onclick="window.location.reload()">取消</div>
        </div>
 </script>
<script language="javascript">
           function address_sub1(){
          require(['tpl', 'core','sweetalert'], function(tpl, core,swal) {
                if($(this).attr('saving')=='1'){
                        return;
                }
                if($('#realname').isEmpty()){
                    core.tip.show('请输入收件人!');
                    return;
                }
                if(!$('#mobile').isMobile()){
                    core.tip.show('请输入正确的联系电话!');
                    return;
                }
                if($('#address').isEmpty()){
                    core.tip.show('请输入收货地址!');
                    return;
                } 
                $('.address_sub1').html('正在处理...').attr('saving',1);
                core.pjson('goldshop/detail',{
                    op:'submit',
                    id:$('#addressid').val(),
                    addressdata: {
                        realname: $('#realname').val(),
                        mobile: $('#mobile').val(),
                        address: $('#address').val(),
                        province: $('#sel-provance').val(),
                        city: $('#sel-city').val(),
                        area: $('#sel-area').val()
                        //,zipcode: $('#zipcode').val()
                    }
                 },function(json){
                     if(json.status==1){
                         core.tip.show('保存成功!');
                         list();
                     }
                     else{
                         $('.address_sub1').html('确认').removeAttr('saving');
                         core.tip.show('保存失败');
                     }
                },true,true);
            });
    }
                function addaddress(){
                 var n=$('#btnsub').attr("class");
                 if(n=="sub"){
                   $('#showform').hide(); 
                   $('#addform').show();
                   cascdeInit("","","");
                 }else{
                   return;  
                 } 
                }
    window.logid = '';
    window.canbuy = false;
    window.timer = 0;
    require(['tpl', 'core','sweetalert'], function(tpl, core,swal) {
        function lottery(goods){
              var type = goods.type;
               if(type==0){
                                        
                                          core.pjson('goldshop/detail' ,{'op':'lottery','id':"{$id}",'logid':window.logid},function(json){
                                             
                                              if(json.status==2){  setTimeout(function(){
                                                 swal({ 'title':'',text:'恭喜您，兑换成功!', imageUrl:'../addons/ewei_shop/plugin/creditshop/template/mobile/default/images/lo1.gif', 
                                                  confirmButtonText: '确 定',
                                                  confirmButtonColor:'#f90' },function(){
                                                      location.href =core.getUrl('plugin/goldshop/log',{shine:1});
                                                  });   },1);
                                              }
                                              else{
                                                  core.tip.show( json.result);
                                              }
                                          },true,true);
                                       
                             
               }else{
                    swal({   title: "",   text: "努力抽奖中，请稍后....",  imageUrl:'../addons/ewei_shop/plugin/goldshop/template/mobile/default/images/lo.gif',  showConfirmButton: false });

                                   setTimeout(function(){
                                       core.pjson('goldshop/detail' ,{'op':'lottery','id':"{$id}",'logid':window.logid},function(json){
                                           if(json.status==-1){
                                                swal({ 'title':'',text:json.result,confirmButtonText: '确 定',confirmButtonColor:'#f90'},function(){
                                                     
                                                });
                                           }
                                           else if(json.status==2){
                                              swal({ 'title':'',text:'恭喜您, 您中奖啦!', imageUrl:'../addons/ewei_shop/plugin/goldshop/template/mobile/default/images/lo1.gif', 
                                                  confirmButtonText: '确 定',
                                                  confirmButtonColor:'#f90' },function(){
                                                      location.href =core.getUrl('plugin/goldshop/log',{shine:1});
                                                  });
                                            } else {
                                                swal({ 'title':'',text:'很遗憾, 您没有中奖!',confirmButtonText: '确 定',confirmButtonColor:'#f90'},function(){
                                                      location.reload();
                                                });
                                            }
                                       },false,true);
                                 },1000);
                 }
          
        }
        function pay(goods){
           
                            core.pjson('goldshop/detail' ,{
                                'op':'pay',
                                'id':"{$id}",
                                addressdata: {
                                    realname: $('#realname').val(),
                                    mobile: $('#mobile').val(),
                                    address: $('#address').val(),
                                    province: $('#sel-provance').val(),
                                    city: $('#sel-city').val(),
                                    area: $('#sel-area').val()
                                    //,zipcode: $('#zipcode').val()
                                }
                            },function(json){
                                if(json.status!=1){
                                    core.tip.show(json.result);
                                    return;
                                }
                           
                                var result = json.result;
                                window.logid = result.logid;
                                
                                if(result.wechat){
                                    var wechat = result.wechat;
                                     require(['http://res.wx.qq.com/open/js/jweixin-1.0.0.js'],function(wx){
                                        jssdkconfig = {php echo json_encode($_W['account']['jssdkconfig']);} || { jsApiList:[] };
                                        jssdkconfig.debug = false;
                                        jssdkconfig.jsApiList = ['checkJsApi','chooseWXPay'];
                                        wx.config(jssdkconfig);
	                      wx.ready(function () {
                                                $('.button').removeAttr('submitting');
                                                 var appid = wechat.appid?wechat.appid:wechat.appId;
                                                wx.chooseWXPay({
                                                    'appId' :  appid,
                                                    'timestamp': wechat.timeStamp,
                                                    'nonceStr' : wechat.nonceStr,
                                                    'package' :  wechat.package,
                                                    'signType' : wechat.signType,
                                                    'paySign' : wechat.paySign,
                                                    success: function (res) {
                                                             lottery(goods);
                                                    },fail:function(res){
                                                        alert(res.errMsg);
                                                    }
                                                });
                                            });
                                     });
                                } else{
                                    lottery(goods);
                                }
                         
                            },true,true);
                  
        }
        function setTimer(goods){
            
            setTimeHtml(goods);
            window.timer = setInterval(function(){
                setTimeHtml(goods);
            },1000);
        }
        function setTimeHtml(goods){
            var current = Math.floor(new Date().getTime()/1000);
                var ts = 0;//计算剩余的毫秒数
                var prefix = '';
                
                if(goods.timestate=='before'){
                    ts = goods.timestart - current;
                    prefix = "距离活动: ";
                    if(ts<=0){
                        goods.timestate='after';
                        $('#btnsub').removeClass('sub2').addClass('sub').html( goods.type==0?'立即兑换':'立即抽奖');
                        window.canbuy = true;
                    }
                }
                else if(goods.timestate=='after'){
                    ts = goods.timeend - current;
                    
                    prefix = "活动剩余: ";
                    if(ts<=0){
                        clearInterval(window.timer);
                        window.canbuy =false;
                        $('.info_timestate').remove();
                        $('#btnsub').removeClass('sub').addClass('sub2').html( '活动已结束' );
                    }
                }
                var dd = parseInt(ts / 60 / 60 / 24, 10);//计算剩余的天数
                var hh = parseInt(ts  / 60 / 60 % 24, 10);//计算剩余的小时数
                var mm = parseInt(ts  / 60 % 60, 10);//计算剩余的分钟数
                var ss = parseInt(ts % 60, 10);//计算剩余的秒数
                dd = checkTime(dd);
                hh = checkTime(hh);
                mm = checkTime(mm);
                ss = checkTime(ss);
                $('.info_timestate').html(prefix+ "<span>" + dd + "</span>天<span>" + hh + "</span>时<span>" + mm + "</span>分<span>" + ss + "</span>秒");
        }
        function checkTime(i)    
            {    
               if (i < 10) {    
                   i = "0" + i;    
                }    
               return i;    
            } 
        core.pjson('goldshop/detail',{id:"{$id}"},function(json){
            var result = json.result;  
            var goods = result.goods;
            if(!goods){
                core.message('商品已下架或被删除!',"{php echo $this->createPluginMobileUrl('goldshop')}",'error');
                return;
            }
            window.canbuy = goods.canbuy;
           
            $('#container').html(  tpl('tpl_main',result) );
          
             if(goods.timestate!=''){
                         setTimer(goods);
            }
                $('#btnsubaddress').click(function(){
                if($(this).attr('saving')=='1'){
                        return;
                }
                if($('#realname').isEmpty()){
                    core.tip.show('请输入收件人!');
                    return;
                }
                if(!$('#mobile').isMobile()){
                    core.tip.show('请输入正确的联系电话!');
                    return;
                }
                if($('#address').isEmpty()){
                    core.tip.show('请输入收货地址!');
                    return;
                } 
                $('.address_sub1').html('正在处理...').attr('saving',1);
                 if (!window.canbuy){
                        return;
                 }
                 if(goods.followneed=='1' && !result.followed){
                        swal({   title: "提示",   text: goods.followtext ,    confirmButtonText: '立即去关注',
                                    confirmButtonColor:'#f90' },function(){
                                    location.href = goods.followurl;
                          });
                        return;
                  } 
                 pay(goods);
               //  if(goods.type==0){
                //       swal({ 'title':'',text:'确认要兑换吗？',
                //                       confirmButtonText: '确 定',
                //                       confirmButtonColor:'#f90' ,
                 //                      cancelButtonText: '取 消',
                //                        showCancelButton: true,
                //                   },function(isConfirm){
                //                    if(isConfirm){
                //                         pay(goods);   
                //                    }
                //       });
                //       return;
               //  }else{
                //     pay(goods);   
                // }
              });
           
            
        },true);
        
    })
</script>
{template 'common/footer'}